<?php

namespace qore_base\util\collections;

/**
 * MapIterator implements an interator for {@link qore\util\collections\Map}.
 *
 * @author Samuel Nogueira <samuel.nogueira.dev@gmail.com>
 */
class MapIterator implements \Iterator {

	/**
	 * @var array the data to be iterated through
	 */
	private $_data;

	/**
	 * @var array list of keys in the map
	 */
	private $_keys;

	/**
	 * @var mixed current key
	 */
	private $_key;

	/**
	 * Constructor.
	 * @param array $data the data to be iterated through
	 */
	public function __construct(&$data) {
		$this->_data = &$data;
		$this->_keys = array_keys($data);
		$this->_key = reset($this->_keys);
	}

	/**
	 * Rewinds internal array pointer.
	 * This method is required by the interface Iterator.
	 */
	public function rewind() {
		$this->_key = reset($this->_keys);
	}

	/**
	 * Returns the key of the current array element.
	 * This method is required by the interface Iterator.
	 * @return mixed the key of the current array element
	 */
	public function key() {
		return $this->_key;
	}

	/**
	 * Returns the current array element.
	 * This method is required by the interface Iterator.
	 * @return mixed the current array element
	 */
	public function current() {
		return $this->_data[$this->_key];
	}

	/**
	 * Moves the internal pointer to the next array element.
	 * This method is required by the interface Iterator.
	 */
	public function next() {
		$this->_key = next($this->_keys);
	}

	/**
	 * Returns whether there is an element at current position.
	 * This method is required by the interface Iterator.
	 * @return boolean
	 */
	public function valid() {
		return $this->_key !== false;
	}

}
